package ceri.m2.project.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ceri.m2.project.freebase.Article;

/**
 * Servlet implementation class Wrapper
 */
public class Wrapper extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Article article;

    /**
     * Default constructor. 
     */
    public Wrapper() {
    	article = new Article();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();		
		String op = request.getParameter("op");
		String name = request.getParameter("name");
		String id = request.getParameter("id");
		
		if(op.equalsIgnoreCase("getimages"))
		{
			out.write(article.getGuidImage(name, id).toString());
		}
		else
			out.write("Operation not found");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();		
		String op = request.getParameter("op");
		String name = request.getParameter("name");
		String id = request.getParameter("id");
		
		if(op.equalsIgnoreCase("getimages"))
		{
			out.write(article.getGuidImage(name, id).toString());
		}
		else if(op.equalsIgnoreCase("getdescription"))
		{
			out.write(article.getDescription(name, id).toString());
		}
		else
			out.write("Operation not found");
	}
}


